package com.hk.manager.upload;

import cn.hutool.core.util.ObjectUtil;
import com.hk.exception.BusinessException;
import com.hk.exception.ErrorCode;
import com.hk.exception.ThrowExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author huangkun
 * @date 2024/12/25 20:34
 */
@Slf4j
@Component
public class FileUploadHandler extends UploadHandler<MultipartFile> {


    @Override
    protected void handleFile(MultipartFile inputSource, File tempFile) {
        ThrowExceptionUtil.throwIf(inputSource == null || tempFile == null, ErrorCode.ERROR_SYSTEM, "文件不能为空");
        try {
            inputSource.transferTo(tempFile);
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.ERROR_SYSTEM, "上传失败");
        }
    }

    @Override
    protected String getOriginalFileName(MultipartFile inputSource) {
        ThrowExceptionUtil.throwIf(ObjectUtil.isNull(inputSource), ErrorCode.ERROR_SYSTEM, "文件不能为空");
        return inputSource.getOriginalFilename();
    }

    @Override
    protected void validateFile(MultipartFile inputSource) {

    }
}
